package com.personalblog.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/22 - 13:25
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Blog implements Serializable {
    /**
     * 实体类实现序列化接口
     */
    private static final long serialVersionUID = 1L;

    private Integer blogId;
    private Boolean blogAppreciation;
    private Boolean blogCommentabled;
    private String blogContent;
    private Date blogCreateTime;
    private String blogFirstPicture;
    private String blogFlag;
    private Boolean blogShareStatement;
    private String blogTitle;
    private Date blogUpdateTime;
    private Integer blogViews;
    private Boolean blogPublished;
    private Boolean blogRecommend;
    private String blogDescription;
    private User user;
    //private Integer userId;
    private Type type;
    //private Integer typeId;
    private List<Tag> tagList;
    private String tagIds;

    public String formateTime(Date date) {
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }
    public void init() {
        this.tagIds = tagsToIds(this.getTagList());
    }

    //1,2,3
    private String tagsToIds(List<Tag> tags) {
        if (!tags.isEmpty()) {
            StringBuffer ids = new StringBuffer();
            boolean flag = false;
            for (Tag tag : tags) {
                if (flag) {
                    ids.append(",");
                } else {
                    flag = true;
                }
                ids.append(tag.getTagId());
            }
            return ids.toString();
        } else {
            return tagIds;
        }
    }
}
